using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ThaiLanguageTools.DataStruct;

namespace ThaiLanguageTools.SrcParse
{
    public class MappingsParser
    {
        /// <summary>
        /// Parse a line and return an entry
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        public delegate String ParseLineDel(String line);

        public static String Trim(String line) { return line.Trim(); }

        static String SpiltAtColonPart1(String line)
        {
            int cidx = line.IndexOf(":");
            if (cidx < 0) { return line.Trim(); }

            String str = line.Substring(0, cidx);
            return str.Trim();
        }
        static String SpiltAtColonPart2(String line)
        {
            int cidx = line.IndexOf(":");
            if (cidx < 0) { return line.Trim(); }

            String str = line.Substring(cidx + 1);
            return str.Trim(); 
        }
        static String CleanUpRom(String line)
        {
            line = SpiltAtColonPart1(line);
            line = line.ToLower(); // De-capitalize
            
            // Skip complex stuff with brackets etc. -- we'll never search for it anyway
            if (ContainsAny(line, "(", ")", "{", "}", "?")) { return null; }

            line = line.Replace(" ", ""); // Remove spaces

            if(line.Length == 0) { return null; }

            // unlikely to type over this many chars and get them right
            const int MaxLength = 30; 
            if (line.Length > MaxLength) { return null; }

            return line;
        }

        static bool ContainsAny(String s, params String[] parts)
        {
            foreach (String p in parts)
            {
                if (s.Contains(p)) { return true; }
            }
            return false;
        }

        public StringMapping ParseAndCleanUp(String file)
        {
            return ParseParallelFiles(file, file + ".rom", Trim, CleanUpRom);
        }

        public StringMapping ParseRomIsPart2(String file)
        {
            return ParseParallelFiles(file, file + ".rom", Trim, SpiltAtColonPart2);
        }
        public StringMapping ParseRomIsPart1(String file)
        {
            return ParseParallelFiles(file, file + ".rom", Trim, SpiltAtColonPart1);
        }

        public StringMapping ParseParallelFiles(String fileA, String fileB,
            ParseLineDel parseLineA, ParseLineDel parseLineB)
        {
            Console.WriteLine("Parsing parallel files: " + fileA + " and " + fileB);

            StringMapping sm = new StringMapping();
            using (StreamReader srA = new StreamReader(fileA, Utils.ThaiEncoding))
            {
                using (StreamReader srB = new StreamReader(fileB, Utils.ThaiEncoding))
                {
                    int lineNum = 0;
                    while (true)
                    {
                        String lineA = srA.ReadLine();
                        String lineB = srB.ReadLine();

                        if (lineA == null || lineB == null) 
                        {
                            if (lineA != null || lineA != null)
                            {
                                Console.WriteLine("Files do not have same # of lines, stopping at: " + lineNum);
                            }
                            break; 
                        }

                        String a = parseLineA(lineA);
                        String b = parseLineB(lineB);
                        if (a != null && b != null) { sm.Add(a, b); }

                        lineNum++;
                        if (lineNum % 100 == 0)
                        {
                            Console.WriteLine(lineNum + ") " + a + " -> " + b);
                        }
                    }
                    Console.WriteLine("Done.");
                }
            }
            return sm;
        }

    }
}
